<template>
  <div class="login">


    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="账号" prop="username">
        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login()">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {post} from "../service/api";

const axios = require("axios")
export default {
  name: "Login",
  data() {
    var valiaccount = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      } else {
        callback();
      }
    };
    var valipassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {validator: valiaccount, trigger: 'blur'}
        ],
        password: [
          {validator: valipassword, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          return;
        }
        post('/account/login', {username: this.ruleForm.username, password: this.ruleForm.password}, 'form')
          .then(result => { // 正确的返回结果;
            // console.log(result);
            // sessionStorage.setItem("loginAccount",result)
            this.$router.push('/user/index') //跳转到首页
          }).catch(e => { // 执行失败的结果
          this.$message(e.message)
        })
      });
    }
  }
}
</script>
<style scoped>
.login {
  max-width: 600px;
  margin: 50px auto;
}
</style>